package com.SPOooON.googletaskssync;

import android.content.ContentValues;
import android.database.Cursor;

import com.google.api.client.util.Data;
import com.google.api.client.util.DateTime;
import com.google.api.services.tasks.model.Task;

public class TaskWrapper {
	
	private int _id;
	private int taskList;
	private Boolean newTask;
	private Task task;
	private String account;
	private int previous;
	
	public TaskWrapper(){
		this.task = new Task();
		this.task.setStatus(GoogleTasksContentProvider.Tasks.Status.NEEDSACTION);
		this.task.setKind("task#task");
		this.task.setTitle("new Task");
	}
	
	public TaskWrapper(Cursor c) {
		this();
		this.setFromCursor(c);
	}
		
	public TaskWrapper(String account, int taskList, Task task) {
		this.account = account;
		this.task = task;
		this.taskList = taskList;
		this.newTask = false;
	}
	
	public void setFromCursor(Cursor c){
		
		String id = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.ID));
		String eTag = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.ETAG));
		String title = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.TITLE));
		String selfLink = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.SELFLINK));
		String parent = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.PARENT));
		String position = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.POSITION));
		String notes = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.NOTES));
		String status = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.STATUS));
		boolean deleted = (c.getInt(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.DELETED)) == 0 ? false : true);
		boolean hidden = (c.getInt(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.HIDDEN)) == 0 ? false : true);
		
		long updatedLong = c.getLong(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.UPDATED));
		long dueLong = c.getLong(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.DUE));
		long completedLong = c.getLong(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.COMPLETED));
						
		DateTime updated = updatedLong > 0 ? new DateTime(updatedLong, 0) : null;
		DateTime due = dueLong > 0 ? new DateTime(dueLong, 0) : null;
		DateTime completed = completedLong > 0 ? new DateTime(completedLong, 0) : null;

		_id = c.getInt(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns._ID));
		taskList = c.getInt(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.TASKLIST));
		newTask = (c.getInt(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.NEW)) == 0 ? false : true);
		account = c.getString(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.ACCOUNT));
		previous = c.getInt(c.getColumnIndex(GoogleTasksContentProvider.Tasks.Columns.PREVIOUS));
				
		task.setId(id);
		task.setEtag(eTag);
		task.setTitle(title);
		task.setUpdated(updated);
		task.setSelfLink(selfLink);
		task.setParent(parent);
		task.setPosition(position);
		task.setNotes(notes);
		task.setStatus(status);
		task.setDue(due);
		task.setCompleted(completed);
		task.setDeleted(deleted);
		task.setHidden(hidden);
	}
	
	public ContentValues getContentValues(){
		
		ContentValues cv = new ContentValues();
		
		cv.put(GoogleTasksContentProvider.Tasks.Columns.ID, task.getId());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.ETAG, task.getEtag());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.TITLE, task.getTitle());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.UPDATED, (task.getUpdated() == null) ? null : task.getUpdated().getValue());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.SELFLINK, task.getSelfLink());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.PARENT, task.getParent());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.POSITION, task.getPosition());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.NOTES, task.getNotes());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.STATUS, task.getStatus());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.DUE, (task.getDue() == null) ? null : task.getDue().getValue());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.COMPLETED, (task.getCompleted() == null) ? null : task.getCompleted().getValue());
		cv.put(GoogleTasksContentProvider.Tasks.Columns.DELETED, task.getDeleted() == null ? 0 : task.getDeleted() ? 1 : 0);
		cv.put(GoogleTasksContentProvider.Tasks.Columns.HIDDEN, task.getHidden() == null ? 0 : task.getHidden() ? 1 : 0);
		
		cv.put(GoogleTasksContentProvider.Tasks.Columns.NEW, (newTask) ? 1 : 0);
		cv.put(GoogleTasksContentProvider.Tasks.Columns.TASKLIST, taskList);
		cv.put(GoogleTasksContentProvider.Tasks.Columns.ACCOUNT, account);
		cv.put(GoogleTasksContentProvider.Tasks.Columns.PREVIOUS, previous);
		
		return cv;
	}
	
	public void invertStatus(){
		String status = task.getStatus();
		
		if (status.equals(GoogleTasksContentProvider.Tasks.Status.COMPLETED)) {
			task.setStatus(GoogleTasksContentProvider.Tasks.Status.NEEDSACTION);
			task.setCompleted(null);
		} else {
			task.setStatus(GoogleTasksContentProvider.Tasks.Status.COMPLETED);
			task.setCompleted(GoogleTasksContentProvider.getTimeStamp());
		}
		
		task.setUpdated(GoogleTasksContentProvider.getTimeStamp());
	}
	
	public Task getTask() {		
		return this.task;
	}
	
	public int getTaskList() {		
		return taskList;
	}
	
	public void setNew(boolean newTask) {		
		this.newTask = newTask;
	}
	
	public boolean getNew(){		
		return this.newTask;
	}
	
	public int get_id(){
		return _id;
	}
	
	public void setPrevious(int previous) {
		this.previous = previous;
	}
	
	public int getPrevious() {
		return this.previous;
	}
}
